package com.sxhuayuan.parking.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sxhuayuan.parking.utils.SettingUtils;

/**
 * 出入口(车道)
 * 
 * @author gy
 */
@Entity
@Table(name = "hypk_park_channel")
public class ParkChannel extends BaseEntity {

	private static final long serialVersionUID = 7820755788452993649L;

	/** session中缓存的出口编号 */
	public static final String SESSION_ATTR_NAME = "session_channel_sn";

	/** 名称 */
	private String name;

	/** 第三方出入口通道编号 */
	private String channelSn;

	/** 车场出口控制设备的id */
	private String deviceId;

	private Type type;

	/** 出口标识 */
	private String sn;

	/** 所属车场 */
	private ParkingLot parkingLot;

	private Agent agent;

	public enum Type {

		entrance,

		exit

	}

	@NotEmpty
	@Column(length = 64)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length = 64, nullable = false, updatable = false)
	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	@NotNull
	@JoinColumn(name = "parking_lot_id")
	@ManyToOne(fetch = FetchType.LAZY)
	public ParkingLot getParkingLot() {
		return parkingLot;
	}

	public void setParkingLot(ParkingLot parkingLot) {
		this.parkingLot = parkingLot;
	}

	/** 出口缴费二维码链接 */
	@Transient
	public String getQrcodeUrl() {
		return String.format("%s%s%s%s", SettingUtils.get().getSiteUrl(), "/member/park/", (type == Type.exit ? "pay/" : "in/"), getSn());
	}

	@Column(length = 64)
	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	@JsonIgnore
	@ManyToOne(fetch = FetchType.LAZY)
	public Agent getAgent() {
		return agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	@NotNull
	@Column(nullable = false)
	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	@Column(length = 20)
	public String getChannelSn() {
		return channelSn;
	}

	public void setChannelSn(String channelSn) {
		this.channelSn = channelSn;
	}
}
